<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>canvas</title>
  <style>
    #chart {
      background-color: gray;
    }
  </style>
</head>

<body>
  <canvas id="chart" width="600" height="500"></canvas>

  <script>
    const canvas = document.getElementById('chart')
    const ctx = canvas.getContext('2d')

    // 标题
    ctx.font = "30px 宋体"
    ctx.textAlign = "left"
    ctx.fillText('柱形图', 20, 50)

    // 坐标轴 x-y
    ctx.moveTo(100, 100)
    ctx.lineTo(100, 350)
    ctx.lineTo(500, 350)
    ctx.stroke()

    // y轴刻度线
    ctx.moveTo(100, 100)
    ctx.lineTo(90, 100)

    ctx.moveTo(100, 150)
    ctx.lineTo(90, 150)

    ctx.moveTo(100, 200)
    ctx.lineTo(90, 200)

    ctx.moveTo(100, 250)
    ctx.lineTo(90, 250)

    ctx.moveTo(100, 300)
    ctx.lineTo(90, 300)

    ctx.moveTo(100, 350)
    ctx.lineTo(90, 350)

    // x轴刻度线
    ctx.moveTo(100, 350)
    ctx.lineTo(100, 360)

    ctx.moveTo(200, 350)
    ctx.lineTo(200, 360)

    ctx.moveTo(300, 350)
    ctx.lineTo(300, 360)

    ctx.moveTo(400, 350)
    ctx.lineTo(400, 360)

    ctx.moveTo(500, 350)
    ctx.lineTo(500, 360)

    ctx.stroke()

    // 刻度线的值
    ctx.font = '12px 宋体'
    ctx.textAlign = 'right'
    ctx.fillText(150, 88, 105)
    ctx.fillText(120, 88, 155)
    ctx.fillText(90, 88, 205)
    ctx.fillText(60, 88, 255)
    ctx.fillText(30, 88, 305)
    ctx.fillText(0, 88, 355)

    ctx.font = '14px 宋体'
    ctx.textAlign = 'center'
    ctx.fillText('食品', 150, 370)
    ctx.fillText('数码', 250, 370)
    ctx.fillText('服饰', 350, 370)
    ctx.fillText('箱包', 450, 370)

    // 线
    ctx.strokeStyle = 'rgba(0, 0, 0, .5)'
    ctx.moveTo(100, 100)
    ctx.lineTo(500, 100)

    ctx.moveTo(100, 150)
    ctx.lineTo(500, 150)

    ctx.moveTo(100, 200)
    ctx.lineTo(500, 200)

    ctx.moveTo(100, 250)
    ctx.lineTo(500, 250)

    ctx.moveTo(100, 300)
    ctx.lineTo(500, 300)
    ctx.stroke()

    // 矩形
    /*
      x = 前面的值 + 10
      y = 350 - height
      width = 80
      height = 矩形的值 * 250 / 150
    */

    ctx.fillStyle = 'pink'
    const h1 = 100 * 250 / 150
    const y1 = 350 - h1 - 1
    ctx.fillRect(110, y1, 80, h1)

    const h2 = 120 * 250 / 150
    const y2 = 350 - h2 - 1
    ctx.fillRect(210, y2, 80, h2)

    const h3 = 90 * 250 / 150
    const y3 = 350 - h3 - 1
    ctx.fillRect(310, y3, 80, h3)

    const h4 = 150 * 250 / 150
    const y4 = 350 - h4 - 1
    ctx.fillRect(410, y4, 80, h4)

  </script>
</body>

</html>